import javax.media.opengl.GL;
/*
 * Tomer Nuni 300585312
 * Idan Meshita 300043726
 */
import com.sun.opengl.util.texture.Texture;


public class BenchBuilder {
	
	public Texture legsTexture;
	public Texture topTexture;
	public Texture frameTexture;
	private int bench;
	
	public int length = 600;
	public int height = 85;
	public int width = 60;
	public int legWidth = 10;
	public int frameWidth = 20;
	
	
	public void buildTable(GL gl){
		make_table(gl,length, height, width, legWidth, frameWidth);
	}
	
	private void make_leg(GL gl,double x,double y,double z, double legWidth, double legHeight){
        gl.glPushMatrix();
        gl.glTranslated(x, y, z);
        legsTexture.bind();
		// draw leg
		gl.glBegin(GL.GL_QUAD_STRIP);
		 		
		
		gl.glTexCoord2f(0.0f, 0.0f);
		gl.glVertex3d(0, 0, 0);
	
		gl.glTexCoord2f(0.0f,3.0f);
		gl.glVertex3d(0, legHeight, 0);
		
		
		gl.glTexCoord2f(12.0f, 0.0f);
		gl.glVertex3d(legWidth, 0, 0);
		
		gl.glTexCoord2f(12.0f, 3.0f);
		gl.glVertex3d(legWidth, legHeight, 0);

		gl.glTexCoord2f(0.0f, 0.0f);
		gl.glVertex3d(legWidth, 0, legWidth);
		
		gl.glTexCoord2f(0, 3.0f);
		gl.glVertex3d(legWidth, legHeight, legWidth);
		
		gl.glTexCoord2f(12.0f, 0.0f);
		gl.glVertex3d(0, 0, legWidth);
		
		gl.glTexCoord2f(12.0f, 3.0f);
		gl.glVertex3d(0, legHeight, legWidth);
		
		gl.glTexCoord2f(0.0f, 0.0f);
		gl.glVertex3d(0, 0, 0);
	
		gl.glTexCoord2f(0.0f,3.0f);
		gl.glVertex3d(0, legHeight, 0);
		
		gl.glEnd();
		
		gl.glPopMatrix();
	}	

	private void make_frame(GL gl,double sizeX,double sizeY,double sizeZ, double frameWidth){
		frameTexture.bind();
		// draw frame
		gl.glBegin(GL.GL_QUAD_STRIP);
		 		
        
		gl.glTexCoord2f(0.0f, 0.0f);
		gl.glVertex3d(0, sizeY - frameWidth , 0);
	
		gl.glTexCoord2f(0.0f,3.0f);
		gl.glVertex3d(0, sizeY, 0);
		
		gl.glTexCoord2f(1.0f, 0.0f);
		gl.glVertex3d(sizeX, sizeY - frameWidth , 0);
	
		gl.glTexCoord2f(1.0f,3.0f);
		gl.glVertex3d(sizeX, sizeY, 0);
	
		gl.glTexCoord2f(0.0f, 0.0f);
		gl.glVertex3d(sizeX, sizeY - frameWidth , sizeZ);
	
		gl.glTexCoord2f(0.0f,3.0f);
		gl.glVertex3d(sizeX, sizeY, sizeZ);
		
		gl.glTexCoord2f(1.0f, 0.0f);
		gl.glVertex3d(0, sizeY - frameWidth , sizeZ);
	
		gl.glTexCoord2f(1.0f,3.0f);
		gl.glVertex3d(0, sizeY, sizeZ);
		
		gl.glTexCoord2f(0.0f, 0.0f);
		gl.glVertex3d(0, sizeY - frameWidth , 0);
	
		gl.glTexCoord2f(0.0f,3.0f);
		gl.glVertex3d(0, sizeY, 0);
				
		gl.glEnd();
		
		
	}	

	private void make_top(GL gl,double sizeX,double sizeY,double sizeZ){
        
		// draw top
        topTexture.bind();
		gl.glBegin(GL.GL_QUAD_STRIP);
		 		
		gl.glTexCoord2f(0.0f, 0.0f);
		gl.glVertex3d(0, sizeY , 0);
	
		gl.glTexCoord2f(0.0f, 40.0f);
		gl.glVertex3d(sizeX, sizeY, 0);
		
		gl.glTexCoord2f(40.0f, 0.0f);
		gl.glVertex3d(0, sizeY , sizeZ);
	
		gl.glTexCoord2f(40.0f, 40.0f);
		gl.glVertex3d(sizeX, sizeY, sizeZ);
	
//		gl.glTexCoord2f(0.0f,4.0f);
//		gl.glVertex3d(0, sizeY, 0);
				
		gl.glEnd();
		
		
	}	
	
	private void make_table(GL gl,double sizeX,double sizeY,double sizeZ, double legWidth, double frameWidth){
		
		bench = gl.glGenLists(1);

		gl.glNewList(bench, GL.GL_COMPILE);

        gl.glTexParameteri ( GL.GL_TEXTURE_2D,GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT );
        gl.glTexParameteri( GL.GL_TEXTURE_2D,GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT );
        

        // draw legs
        make_leg(gl, 0, 0, 0, legWidth, sizeY - frameWidth);                
        make_leg(gl, sizeX - legWidth, 0, 0, legWidth, sizeY - frameWidth);
        make_leg(gl, sizeX - legWidth, 0, sizeZ - legWidth, legWidth, sizeY - frameWidth);
        make_leg(gl, 0, 0, sizeZ - legWidth, legWidth, sizeY - frameWidth);
		
        // draw frame
        make_frame(gl, sizeX, sizeY, sizeZ, frameWidth);
        
        // draw top
        make_top(gl, sizeX, sizeY, sizeZ);

	    gl.glEndList();

	}
 
	public void draw(GL gl) {
		gl.glCallList(bench);
		
	}
}
